package com.ly.autosignin.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ly.autosignin.common.utils.ScheduleUtils;
import com.ly.autosignin.entity.Task;
import com.ly.autosignin.mapper.TaskMapper;
import com.ly.autosignin.service.ITaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘垚
 * @since 2020-08-14
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {
    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private Scheduler scheduler;


    @Override
    @PostConstruct
    public void initSchedule() throws SchedulerException {
        Task task=new Task();
        task.setCreateBy(1L);
        List<Task> tasks=selectTaskByUId(task);
        for (Task elment:tasks){
            ScheduleUtils.createSchedule(scheduler,elment);
        }
        scheduler.start();
    }

    @Override
    public List<Task> selectTaskByUId(Task task) {
        return  taskMapper.selectList(new QueryWrapper<Task>().lambda()
                .eq(Task::getCreateBy, task.getCreateBy()));
    }
}
